<?php

namespace Admin\Controller;
use Think\Controller;
class SiteController extends BaseController {

	public function pk10(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function agent(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}



	public function message(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
		
		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}
	
	public function index() {
		if (IS_POST) {
			$this -> sitesave('site.php');
		} else {
			$this -> display();
		}
	}

	public function setting() {
		if (IS_POST) {
			$this -> sitesave('route.php');
		} else {
			$this -> display();
		}
	}

	private function sitesave($filename) {

		if ($this -> update_config($_POST, $filename)) {

			$this -> success('修改成功，跳转中~', U('site/' . ACTION_NAME));

		} else {

			$this -> error('操作失败', U('site/' . ACTION_NAME));

		}
	}

	private function update_config($new_config, $filename) {
		if (isset($new_config['min_point'])) {
			$log = array(
				'username' => session('admin')['username'],
				'type' => 4,
				'addtime' => time(),
				'content' => "修改下注配置倍率"
			);
			M('admin_log')->add($log);
		}


		$config_file = CONF_PATH . $filename;
		if (is_file($config_file) && is_writable($config_file)) {

			$config =

			require $config_file;

			$config = array_merge($config, $new_config);

			file_put_contents($config_file, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";", LOCK_EX);

			@unlink(RUNTIME_FILE);

			return true;

		} else {

			return false;

		}

	}

// ==========================by TheWay 最后修改日期2017.11.17=======================

	//每日签到
	public function dayCard(){
		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	//百家乐
	public function hundred(){
		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}
    //龙虎斗
    public function longhu(){
        if(IS_POST){
            $this -> sitesave('site.php');
        }else{
            $this->display();
        }
    }

	//一键生成当天数据
	public function insertData(){
		$hundred = M("hundred");
		$day = date("Ymd",time());
		//判断当天是否有数据
		$is_data = $hundred->field("addtime")->order("id desc")->limit(1)->select();
		$data_time = date("Ymd",$is_data[0]['addtime']);
		if($data_time == $day){
			$this->success("生成成功");
		}else{
			$start_time = strtotime($day.C('hundred_start_time'));
			$stop_time = strtotime($day.C('hundred_stop_time'));
			$num_time = $stop_time - $start_time;
			$num = $num_time/30;
			$number = $day."0000";
			for($i=1;$i<=$num;$i++){
				$start_num = $i-1;
				$data[] = array(
					'period_number' => $number+$i,
					'open_time' => $start_time + $start_num*30,
					'stop_time' => $start_time - C("hundred_feng_time") + $i*30 ,
					'add_time'  => time()
				);
				// dump($data);die;
			}
			// dump($data);die;
			$res = $hundred->addAll($data);
			if($res){
				$this->success("生成成功");
			}else{
				$this->error("生成失败");
			}
		}
	}

	//操作开奖的页面 当日列表
	public function hundredList(){
		$hundred = M("hundred");
		$where = "type=0";
		$count = $hundred->where($where)->count();
		$page = new \Think\Page($count,15);
		$show = $page->show();		
		$list = $hundred->where($where)->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();
//		foreach ($list as $k => $v) {
//			$list[$k]['open_time'] = date("Y-m-d H:i:s",$v['open_time']);
//			$list[$k]['stop_time'] = date("Y-m-d H:i:s",$v['stop_time']);
//		}
		$this->assign('list',$list);
		$this->assign('show',$show);
		$this->display();
	}
//操作开奖的页面 当日列表--龙虎
    public function hundredList_lh(){
        $hundred = M("hundred");
        $where = "type=1";
        $count = $hundred->where($where)->count();
        $page = new \Think\Page($count,15);
        $show = $page->show();
        $list = $hundred->where($where)->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();

        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
    }
	//执行
	//id 需要修改的id
	//result 开奖结果
	public function hundredAtion(){
		$id = I('id');
		$result = I('result');
		$info = M("hundred")->where("id='$id'")->save(array('award_numbers'=>$result));
		if($info){
			echo 1;
		}else{
			echo 0;
		}
	}

	//添加公告页面
	public function systemTipAdd(){
		$this->display();
	}

	//添加首页系统公告
	public function systemTipDoAdd(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
    	
		if(IS_POST){
			$content = I('content');
			$new_message = array(
				'type' => 'system_tip',
				'title' => I('title'),
				'head_img_url'=>'/Public/main/img/system.jpg',
				'from_client_name' => '客服',
				'content'=>$content,
				'time'=>date('H:i:s')
			);
			$message = M('message');
			$res = $message->add($new_message);
			if($res){	
				$this->success('成功');
			}else{
				$this->error('失败');
			}
		}else{
			$this->error('失败');
		}		
	}

	//首页系统公告列表
	public function systemTipList(){ 
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
		$message = M("message");
		$where = "type='system_tip'";
		$count = $message->where($where)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();		
		$list = $message->where($where)->select();
		// dump($list);die;
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();	
	}

	//删除公告
	public function systemTipDel(){
		$id = I('id');
		$message = M("message");
		$res = $message->where("id='$id'")->delete();
		if($res){
			$this->success("删除成功");
		}else{
			$this->success("删除失败");
		}
	}

		//添加公告页面
	public function activeAdd(){
		$this->display();
	}

	//添加首页系统公告
	public function activeDoAdd(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
    	
		if(IS_POST){
			$content = I('content');
			$new_message = array(
				'type' => 'active',
				'title' => I('title'),
				'head_img_url'=>'/Public/main/img/system.jpg',
				'from_client_name' => 'admin',
				'content'=>$content,
				'time'=>date('H:i:s')
			);
			$message = M('message');
			$res = $message->add($new_message);
			if($res){	
				$this->success('成功');
			}else{
				$this->error('失败');
			}
		}else{
			$this->error('失败');
		}		
	}

	//首页系统公告列表
	public function activeList(){ 
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
		$message = M("message");
		$where = "type='active'";
		$count = $message->where($where)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();		
		$list = $message->where($where)->select();
		// dump($list);die;
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();	
	}

	//删除公告
	public function activeDel(){
		$id = I('id');
		$message = M("message");
		$res = $message->where("id='$id'")->delete();
		if($res){
			$this->success("删除成功");
		}else{
			$this->success("删除失败");
		}
	}

	// ===========================================================================================================

	public function awardNumbers($num){
		$tmp = range(1,50);
		unset($tmp[0]);
		$data = array_rand($tmp,$num);
		shuffle($data);
		$str = implode(",", $data);
		return $str;
	}	

}
